package com.ruoyi.mobile.service.impl;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.mobile.domain.HuaweiHealthWeekStat;
import com.ruoyi.mobile.domain.UserInfo;
import com.ruoyi.mobile.mapper.UserInfoMapper;
import com.ruoyi.mobile.model.vo.HuaweiHealthMonthStatVo;
import com.ruoyi.mobile.model.vo.HuaweiHealthWeekStatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.HuaweiHealthMonthStatMapper;
import com.ruoyi.mobile.domain.HuaweiHealthMonthStat;
import com.ruoyi.mobile.service.IHuaweiHealthMonthStatService;

/**
 * 华为健康每月统计数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Service
public class HuaweiHealthMonthStatServiceImpl implements IHuaweiHealthMonthStatService 
{
    @Autowired
    private HuaweiHealthMonthStatMapper huaweiHealthMonthStatMapper;
    @Autowired
    public UserInfoMapper userInfoMapper;
    /**
     * 查询华为健康每月统计数据列表
     * 
     * @param huaweiHealthMonthStat 华为健康每月统计数据
     * @return 华为健康每月统计数据
     */
    @Override
    public List<HuaweiHealthMonthStat> selectHuaweiHealthMonthStatList(HuaweiHealthMonthStat huaweiHealthMonthStat)
    {
        return huaweiHealthMonthStatMapper.selectHuaweiHealthMonthStatList(huaweiHealthMonthStat);
    }

    @Override
    public List<HuaweiHealthMonthStatVo> selectHuaweiHealthMonthStatVoList(HuaweiHealthMonthStat huaweiHealthMonthStat)
    {
        List<HuaweiHealthMonthStatVo> list = huaweiHealthMonthStatMapper.selectHuaweiHealthMonthStatVoList(huaweiHealthMonthStat);
        for (HuaweiHealthMonthStatVo huaweiHealthMonthStatVo : list) {
            //获取出生日期
            LocalDate birthday = huaweiHealthMonthStatVo.getBirthday();
            //年龄
            Integer age = null;
            if (birthday == null) {
                age = null;
            } else {
                age = Period.between(birthday, LocalDate.now()).getYears();
            }
            //身高
            String height = "未填写";
            //体重
            String weight = "未填写";
            //获取用户基本信息
            UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(huaweiHealthMonthStatVo.getUserId(), 4, 4);
            if (userInfo != null) {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> map = objectMapper.readValue(userInfo.getContent(), Map.class);
                    height = (String) map.get("quest4_1");
                    weight = (String) map.get("quest4_2");
                } catch (Exception e) {

                }
            }
            huaweiHealthMonthStatVo.setHeight(height);
            huaweiHealthMonthStatVo.setWeight(weight);
            huaweiHealthMonthStatVo.setAge(age);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
            String formattedStatStartDate = huaweiHealthMonthStatVo.getStatStartDate().format(formatter);
            String formattedStatEndDate = huaweiHealthMonthStatVo.getStatEndDate().format(formatter);
            huaweiHealthMonthStatVo.setStatDate( formattedStatStartDate + "-" + formattedStatEndDate );
            String birthdayStr = "未填写";
            String ageStr = "未填写";
            if (birthday != null) {
                birthdayStr = huaweiHealthMonthStatVo.getBirthday().format(formatter);
                ageStr = String.valueOf(age);
            }
            huaweiHealthMonthStatVo.setBirthdayStr(birthdayStr);
            huaweiHealthMonthStatVo.setAgeStr(ageStr);
        }
        return list;
    }


    @Override
    public List<HuaweiHealthMonthStatVo> selectHuaweiHealthLastMonthStatVoList() {
        LocalDate endDate = LocalDateTimeUtil.offset(LocalDateTimeUtil.now().toLocalDate().atStartOfDay(),-1, ChronoUnit.DAYS).toLocalDate();
//        如果是最近一个月，而不是最近30天的话就用这个
//        LocalDate startDate = endDate.minusMonths(1);
        LocalDate startDate = LocalDateTimeUtil.offset(endDate.atStartOfDay(),-29, ChronoUnit.DAYS).toLocalDate();
        List<HuaweiHealthMonthStatVo> list = huaweiHealthMonthStatMapper.selectHuaweiHealthLastMonthStatVoList(startDate, endDate);
        for (HuaweiHealthMonthStatVo huaweiHealthMonthStatVo : list) {
            //获取出生日期
            LocalDate birthday = huaweiHealthMonthStatVo.getBirthday();
            //年龄
            Integer age = null;
            if (birthday == null) {
                age = null;
            } else {
                age = Period.between(birthday, LocalDate.now()).getYears();
            }
            //身高
            String height = "未填写";
            //体重
            String weight = "未填写";
            //获取用户基本信息
            UserInfo userInfo = userInfoMapper.selectByUserIdAndSubType(huaweiHealthMonthStatVo.getUserId(), 4, 4);
            if (userInfo != null) {
                try {
                    ObjectMapper objectMapper = new ObjectMapper();
                    Map<String, Object> map = objectMapper.readValue(userInfo.getContent(), Map.class);
                    height = (String) map.get("quest4_1");
                    weight = (String) map.get("quest4_2");
                } catch (Exception e) {

                }
            }
            huaweiHealthMonthStatVo.setHeight(height);
            huaweiHealthMonthStatVo.setWeight(weight);
            huaweiHealthMonthStatVo.setAge(age);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
            String formattedStatStartDate = startDate.format(formatter);
            String formattedStatEndDate = endDate.format(formatter);
            huaweiHealthMonthStatVo.setStatDate( formattedStatStartDate + "-" + formattedStatEndDate );
            String birthdayStr = "未填写";
            String ageStr = "未填写";
            if (birthday != null) {
                birthdayStr = huaweiHealthMonthStatVo.getBirthday().format(formatter);
                ageStr = String.valueOf(age);
            }
            huaweiHealthMonthStatVo.setBirthdayStr(birthdayStr);
            huaweiHealthMonthStatVo.setAgeStr(ageStr);
        }
        return list;
    }
}
